package cn.wolfcode.wms.web.controller;

import cn.wolfcode.wms.domain.Department;
import cn.wolfcode.wms.page.PageResult;
import cn.wolfcode.wms.qo.QueryObject;
import cn.wolfcode.wms.service.DepartmentService;
import cn.wolfcode.wms.util.RequiredPermission;
import cn.wolfcode.wms.vo.JSONResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;
    @RequiredPermission("部门查询")
    @RequestMapping("/list")
    public String list(Map map, QueryObject qo) {
        PageResult result = departmentService.query(qo);
        System.out.println(qo.getPageSize());
        System.out.println("=========================================");
        map.put("result",result);
        return "department/list";
    }
    @RequiredPermission("部门编辑")
    @RequestMapping("/input")
    public String input(Map map, Long id) {
        if (id != null && id != 0) {
            Department dapt = departmentService.getById(id);
            map.put("dapt", dapt);
        }
        return "/department/input";
    }
    @RequiredPermission("部门删除")
    @RequestMapping("/delete")
    @ResponseBody
    public JSONResult delete(Long id) {
        departmentService.deleteById(id);
        try {

        }catch (Exception e){
            e.printStackTrace();
            return JSONResult.mark(e.getMessage());
        }
        return  JSONResult.success("删除成功");
    }
    @RequiredPermission("部门新增/修改")
    @RequestMapping("/saveOrUpdate")
    public String saveOrUpdate(Department dapt) {
        departmentService.updateOrSave(dapt);
        return "redirect:/department/list.do";
    }
}
